import { get, post, put, del } from '@/utils/request'

// 模型类型枚举
export enum ModelType {
  KnowledgeQA = 'KnowledgeQA',
  Embedding = 'Embedding',
  Rerank = 'Rerank'
}

// 模型来源枚举
export enum ModelSource {
  Local = 'local',
  Cloud = 'cloud'
}

// 模型状态枚举
export enum ModelStatus {
  Active = 'active',
  Downloading = 'downloading',
  Inactive = 'inactive',
  Error = 'error'
}

// 嵌入模型参数接口
export interface EmbeddingParameters {
  dimension: number
  truncate_prompt_tokens: number
}

// 模型参数接口
export interface ModelParameters {
  base_url: string
  api_key: string
  embedding_parameters?: EmbeddingParameters
}

// 模型接口
export interface Model {
  id: string
  tenant_id: number
  name: string
  type: ModelType
  source: ModelSource
  description: string
  parameters: ModelParameters
  is_default: boolean
  status: ModelStatus
  created_at: string
  updated_at: string
  deleted_at: string | null
}

// 创建模型请求接口
export interface CreateModelRequest {
  name: string
  type: ModelType
  source: ModelSource
  description: string
  parameters: ModelParameters
  is_default?: boolean
}

// 更新模型请求接口
export interface UpdateModelRequest {
  name?: string
  description?: string
  parameters?: ModelParameters
  is_default?: boolean
}

// API响应接口
export interface ApiResponse<T> {
  data: T
  success: boolean
  message?: string
  error?: {
    code: string
    message: string
    details?: string
  }
}

// 分页响应接口
export interface PaginatedResponse<T> {
  data: T[]
  success: boolean
  total?: number
  page?: number
  page_size?: number
}

/**
 * 获取模型列表
 */
export function getModelsList(): Promise<ApiResponse<Model[]>> {
  return get('/api/v1/models')
}

/**
 * 获取模型详情
 */
export function getModelById(id: string): Promise<ApiResponse<Model>> {
  return get(`/api/v1/models/${id}`)
}

/**
 * 创建模型
 */
export function createModel(data: CreateModelRequest): Promise<ApiResponse<Model>> {
  return post('/api/v1/models', data)
}

/**
 * 更新模型
 */
export function updateModel(id: string, data: UpdateModelRequest): Promise<ApiResponse<Model>> {
  return put(`/api/v1/models/${id}`, data)
}

/**
 * 删除模型
 */
export function deleteModel(id: string): Promise<ApiResponse<{ message: string }>> {
  return del(`/api/v1/models/${id}`)
}

/**
 * 设置默认模型
 */
export function setDefaultModel(id: string, type: ModelType): Promise<ApiResponse<Model>> {
  return post(`/api/v1/models/${id}/set-default`, { type })
}

/**
 * 测试模型连接
 */
export function testModelConnection(id: string): Promise<ApiResponse<{ status: string; message: string }>> {
  return post(`/api/v1/models/${id}/test`)
}

// 导出所有API函数的集合对象
export const modelsApi = {
  getModelsList,
  getModelById,
  createModel,
  updateModel,
  deleteModel,
  setDefaultModel,
  testModelConnection
}
